<?php

class Utils {
   
    public static function GetRequestParam($key) {
        $value = "";
        try {
            if (isset($_REQUEST[$key]) && !empty($_REQUEST[$key])) {
                $value = $_REQUEST[$key];
            }
        }
        catch(Exception $ex) {
            echo $ex->getMessage() . "<br>" . $ex->getTrace() . "<br>";
        }
        return $value;
    }
	
	


    public static function IsEmpty($value) {
        if($value==null || $value==""){
            return true;
        }
        return false;

    }

   
   function getPath($path)  {  
        if(!is_readable($path))  {  
            is_file($path) or mkdir($path,0700);  
        }  
        return $path;
    } 



    public static function GenerateSessionID() {
        return trtoupper(md5(uniqid(mt_rand() , true)));
    }

 
     public static function FillBean($className = "",$bean = null) {
        
        if($bean==null && $className!=""){
              $bean = AbstractBean::LoadBean($className);
        }
        if($bean != null ){
            $keys = array_keys($_REQUEST);
            $cols = $bean->getcols();
            for ($i = 0; $i < count($keys); $i++) {
                $key = $keys[$i];
                if (array_key_exists($key, $cols)) {
                    $value = stripslashes($_REQUEST[$key]);
                    $bean->set($key, $value);
                }
            }
        }
      
        return $bean;
    }
    
  
    public static function get_contents($link) {
        if (function_exists('curl_init')) {
            $curl = curl_init();
            $timeout = 0;
            curl_setopt($curl, CURLOPT_URL, $link);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $timeout);
            curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
            curl_setopt($curl, CURLOPT_AUTOREFERER, true);
            $buffer = curl_exec($curl);
            curl_close($curl);
            return $buffer;
        } else if (function_exists('file_get_contents')) {
            $buffer = file_get_contents($link);
            return $buffer;
        } else if (function_exists('file')) {
            $buffer = implode('', @file($link));
            return $buffer;
        } else {
            return 0;
        }
    }

    public static function AlphaID($in, $to_num = false, $pad_up = false, $passKey = null) {
        $index = "0123456789abcdefghijklmnopqrstuvwyz";
        if ($passKey !== null) {
            for ($n = 0; $n < strlen($index); $n++) $i[] = substr($index, $n, 1);
            $passhash = hash('sha256', $passKey);
            $passhash = (strlen($passhash) < strlen($index)) ? hash('sha512', $passKey) : $passhash;
            for ($n = 0; $n < strlen($index); $n++) $p[] = substr($passhash, $n, 1);
            array_multisort($p, SORT_DESC, $i);
            $index = implode($i);
        }
        $base = strlen($index);
        if ($to_num) {
            $in = strrev($in);
            $out = 0;
            $len = strlen($in) - 1;
            for ($t = 0; $t <= $len; $t++) {
                $bcpow = bcpow($base, $len - $t);
                $out = $out + strpos($index, substr($in, $t, 1)) * $bcpow;
            }
            if (is_numeric($pad_up)) {
                $pad_up--;
                if ($pad_up > 0) $out-= pow($base, $pad_up);
            }
            $out = sprintf('%F', $out);
            $out = substr($out, 0, strpos($out, '.'));
        } else {
            if (is_numeric($pad_up)) {
                $pad_up--;
                if ($pad_up > 0) $in+= pow($base, $pad_up);
            }
            $out = "";
            for ($t = floor(log($in, $base)); $t >= 0; $t--) {
                $bcp = bcpow($base, $t);
                $a = floor($in / $bcp) % $base;
                $out = $out . substr($index, $a, 1);
                $in = $in - ($a * $bcp);
            }
            $out = strrev($out); // reverse

        }
        return $out;
    }

    public static function guid(){
        if (function_exists('com_create_guid')){
            return com_create_guid();
        }else{
            mt_srand((double)microtime()*10000);//optional for php 4.2.0 and up.
            $charid = strtoupper(md5(uniqid(rand(), true)));
            $hyphen = chr(45);// "-"
            $uuid = chr(123)// "{"
                    .substr($charid, 0, 8).$hyphen
                    .substr($charid, 8, 4).$hyphen
                    .substr($charid,12, 4).$hyphen
                    .substr($charid,16, 4).$hyphen
                    .substr($charid,20,12)
                    .chr(125);// "}"
            return $uuid;
        }
    }


   

     

	
}
?>
